package LawsonTransformJava.pgm;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOPAEmployee;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblPAEmployee;


public class HR511PAEmployee extends DAObject implements DAOPAEmployee {

	private TblPAEmployee pae = null;

	public HR511PAEmployee() {
	    // initialization 
			try {
				// There is no separate output file for PAEmployees.
				// It is assigned to an appropriate Employee record.
				this.output = null;
				init();
			} catch (ClassNotFoundException e) {
			    // Could not find the database driver
				System.out.println("ClassNotFoundException: " + e);
			} catch (SQLException e) {
			    // Could not connect to the database
				System.out.println("SQLException: " + e);
			} catch (IOException e) {
				// Could no write properties file
				System.out.println("IOException: " + e);
			}
	}
	
	@Override
	public TblPAEmployee findEmployee(String employeeNumber) {
//		TblPAEmployee pae = new TblPAEmployee();
		try {
			whereClause = " WHERE EMPLOYEE='" + employeeNumber + "'";
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = stmtData.executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
			return (TblPAEmployee) createObject(rslt, new TblPAEmployee());
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return new TblPAEmployee();
	}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

	@Override
	public int countEmployees() {
		try {
			countRecs = stmtData.executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
			countRecs.next();
			return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
	}

	private void init() throws SQLException, ClassNotFoundException, IOException {
	    // Set the data and xref variables
	    data = DAOFactoryOracleDB.getDAOOracleDB(true);
//		    xref = new DAOFactoryAccessDB();
	    sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
	    stmtData = data.getDataStatement();	    
	    countStmtData = data.getCountStatement();
	    //Set the Date Format for output
	    df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	  }
}